<template>
  <!-- 新增弹框 -->

  <el-dialog
    title="编辑弹层"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :visible.sync="showDialog"
  >
    <!-- 新增角色按钮 -->
    <el-row style="height: 60px" @click="doAdd">
      <el-button
        icon="el-icon-plus"
        size="small"
        type="primary"
        @click="showDialog = true"
      >新增角色</el-button>
    </el-row>
    <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="roleForm.name" />
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input v-model="roleForm.description" />
      </el-form-item>
    </el-form>
    <!-- 底部 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      ules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    async doAdd() {
      try {
        const res = await addRole(this.roleForm)
        console.log(res)
        this.loadRoles()
        this.$message.success('添加成功')
        this.showDialog = false
      } catch (err) {
        this.$message.error('添加失败')
        console.log(err)
      }
    },
    hSave() {
      this.$refs.roleForm.validate(valid => {
        if (valid) {
          // 做具体的添加动作
    	this.doAdd()
        }
      })
    }
  }
}

</script>
